package com.carkeeper.system.controller;

import com.alibaba.fastjson.JSON;
import com.carkeeper.common.utils.PageResult;
import com.carkeeper.common.utils.Result;
import com.carkeeper.common.utils.StatusCode;
import com.carkeeper.system.pojo.Department;
import com.carkeeper.system.pojo.Employee;
import com.carkeeper.system.service.EmployeeService;
import com.carkeeper.system.service.UserService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.naming.Name;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;


    /**
     * 根据id查询
     * 修改员工前的数据回显
     *
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        Map<String, Object> resultMap = employeeService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", resultMap);
    }


    /**
     * 添加员工
     *
     * @param employee
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Employee employee) {
        employeeService.add(employee);
        return new Result(true, StatusCode.OK, "添加成功");
    }


    /**
     * 修改员工
     *
     * @param employee
     * @return
     */
    @PutMapping("/update/{id}")
    public Result update(@RequestBody Employee employee, @PathVariable Long id) {
        employeeService.update(employee);
        return new Result(true, StatusCode.OK, "修改成功");
    }


    /**
     * 锁定/解锁
     *
     * @param employee
     * @param id
     * @return
     */
    @PutMapping("/lock/{id}")
    public Result lock(@RequestBody Employee employee, @PathVariable Long id) {
        return employeeService.lock(employee);
    }


    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result findPage(@RequestParam Map searchMap, @PathVariable int page, @PathVariable int size) {
        Map resultMap = employeeService.findPage(searchMap, page, size);
        return new Result(true, StatusCode.OK, "查询成功", resultMap);
    }
}
